<!doctype html>
<html>
<head>
    <meta charset=utf-8>
    <title>小北问答</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <div class="page-header">
            <h1>小北问答 <small>1202 · 贵校限定版</small></h1>
        </div>
        
        {% with messages = get_flashed_messages() %}
            {% if messages %}
                {% for message in messages %}
                    <div class="alert alert-info">
                        {{ message }}
                    </div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        {% if correct_count>0 %}
            <div class="alert alert-success">
                <p>您已经解出 <b>{{ correct_count }}</b> 题</p>
                {% if flags %}
                    <br>
                    <ul>
                        {% for flag in flags %}
                            <li>{{ flag }}</li>
                        {% endfor %}
                    </ul>
                {% endif %}
            </div>
        {% endif %}

        <div class="well">
            <p><b>说明：</b></p>
            <p>
                当第 n 次提交答案后，需要冷却 2<sup>n-1</sup> 小时才能进行下一次提交。
                <br>
                提交答案后，可以在页面底部看到哪些答案正确。
            </p>
        </div>

        <form action="?" method="post" onsubmit="return confirm_submit()">
            {% for p in problemset %}
                <div class="panel panel-default">
                    <div class="panel-heading">
                        #{{ p.id }}
                    </div>
                    <div class="panel-body">
                        <blockquote>
                            {{ p.question_html | safe }}
                        </blockquote>

                        <p>
                            <a href="https://regexper.com/#{{ p.answer_validator|urlencode|replace("/", "%2F") }}" target="_blank" rel="noopener" tabindex="-1">
                                答案格式：
                                <code>{{ p.answer_validator }}</code>
                            </a>
                        </p>
                        <div class="input-group">
                            <div class="input-group-addon">答案</div>
                            <input type="text" class="form-control ggg-answer" placeholder="" name="{{ p.id }}" maxlength="{{ max_length }}" pattern="{{ p.answer_validator }}">
                        </div>
                    </div>
                </div>
            {% endfor %}

            <button class="btn btn-primary btn-block btn-lg" type="submit" {{ 'disabled' if remaining_waiting_s else '' }}>
                {% if remaining_waiting_s is not none %}
                    {{ remaining_waiting_s }} 秒后可以再次提交
                {% else %}
                    提交答案
                {% endif %}
            </button>
            <br>

            {% if history %}
                {% for submission in history %}
                    <div class="well">
                        <p><b>提交时间：</b>{{ submission.time_ts | time }}</p>
                        {% for q in submission.questions %}
                            <p>
                                {% if q.correct %}
                                    <span class="label label-success">正确</span>
                                {% else %}
                                    <span class="label label-danger">错误</span>
                                {% endif %}
                                &nbsp;<b>#{{ q.pid }}：</b>
                                <code>{{ q.answer }}</code>
                            </p>
                        {% endfor %}
                    </div>
                {% endfor %}
            {% endif %}

            <br>
            <br>
        </form>

    </div>

    <script>
        function confirm_submit() {
            if(!confirm("确定要提交吗？请注意关于提交后冷却时间的规定。"))
                return false;
                
            let empty_qs = Array.from(document.querySelectorAll(".ggg-answer")).filter((elem)=>elem.value==="");
            if(empty_qs.length>0) {
                if(!confirm("还有 "+empty_qs.length+" 题未做，仍要提交吗？继续提交将视为本次放弃这些题目。"))
                    return false;
            }
                
            return true;
        }
    </script>
</body>
</html>